<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<title>AngelScript: array template object</title>

<link href="tabs.css" rel="stylesheet" type="text/css"/>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="resize.js"></script>
<script type="text/javascript" src="navtree.js"></script>
<script type="text/javascript">
  $(document).ready(initResizable);
</script>


</head>
<body>
<div id="top"><!-- do not remove this div! -->


<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  
  
  <td style="padding-left: 0.5em;">
   <div id="projectname">AngelScript
   
   </div>
   
  </td>
  
  
  
   
  
 </tr>
 </tbody>
</table>
</div>

<!-- Generated by Doxygen 1.7.5.1 -->
</div>
<div id="side-nav" class="ui-resizable side-nav-resizable">
  <div id="nav-tree">
    <div id="nav-tree-contents">
    </div>
  </div>
  <div id="splitbar" style="-moz-user-select:none;" 
       class="ui-resizable-handle">
  </div>
</div>
<script type="text/javascript">
  initNavTree('doc_addon_array.html','');
</script>
<div id="doc-content">
<div class="header">
  <div class="headertitle">
<div class="title">array template object </div>  </div>
</div>
<div class="contents">
<div class="textblock"><p><b>Path:</b> /sdk/add_on/scriptarray/</p>
<p>The <code>array</code> type is a <a class="el" href="doc_adv_template.html">template object</a> that allow the scripts to declare arrays of any type. Since it is a generic class it is not the most performatic due to the need to determine characteristics at runtime. For that reason it is recommended that the application registers a <a class="el" href="doc_adv_template.html#doc_adv_template_2">template specialization</a> for the array types that are most commonly used.</p>
<p>The type is registered with <code>RegisterScriptArray(asIScriptEngine *engine, bool defaultArrayType)</code>. The second parameter should be set to true if you wish to allow the syntax form <code>type[]</code> to declare arrays.</p>
<h2><a class="anchor" id="doc_addon_array_1"></a>
Public C++ interface</h2>
<div class="fragment"><pre class="fragment"><span class="keyword">class </span>CScriptArray
{
<span class="keyword">public</span>:
  <span class="comment">// Constructor</span>
  CScriptArray(<a class="code" href="angelscript_8h.html#ac8186f029686800b7ce36bde4a55c815" title="32 bit unsigned integer">asUINT</a> length, <a class="code" href="classas_i_object_type.html" title="The interface for an object type.">asIObjectType</a> *ot);
  CscriptArray(<a class="code" href="angelscript_8h.html#ac8186f029686800b7ce36bde4a55c815" title="32 bit unsigned integer">asUINT</a> length, <span class="keywordtype">void</span> *defaultValue, <a class="code" href="classas_i_object_type.html" title="The interface for an object type.">asIObjectType</a> *ot);
  <span class="keyword">virtual</span> ~CScriptArray();

  <span class="comment">// Memory management</span>
  <span class="keywordtype">void</span> AddRef() <span class="keyword">const</span>;
  <span class="keywordtype">void</span> Release() <span class="keyword">const</span>;

  <span class="comment">// Type information</span>
  <a class="code" href="classas_i_object_type.html" title="The interface for an object type.">asIObjectType</a> *GetArrayObjectType() <span class="keyword">const</span>;
  <span class="keywordtype">int</span>            GetArrayTypeId() <span class="keyword">const</span>;
  <span class="keywordtype">int</span>            GetElementTypeId() <span class="keyword">const</span>;

  <span class="comment">// Get the current size</span>
  <a class="code" href="angelscript_8h.html#ac8186f029686800b7ce36bde4a55c815" title="32 bit unsigned integer">asUINT</a> GetSize() <span class="keyword">const</span>;

  <span class="comment">// Resize the array</span>
  <span class="keywordtype">void</span>   Resize(<a class="code" href="angelscript_8h.html#ac8186f029686800b7ce36bde4a55c815" title="32 bit unsigned integer">asUINT</a> numElements);
  
  <span class="comment">// Get a pointer to an element. Returns 0 if out of bounds</span>
  <span class="keywordtype">void</span>  *At(<a class="code" href="angelscript_8h.html#ac8186f029686800b7ce36bde4a55c815" title="32 bit unsigned integer">asUINT</a> index);

  <span class="comment">// Copy the contents of one array to another (only if the types are the same)</span>
  CScriptArray &amp;operator=(<span class="keyword">const</span> CScriptArray&amp;);

  <span class="comment">// Array manipulation</span>
  <span class="keywordtype">void</span> InsertAt(<a class="code" href="angelscript_8h.html#ac8186f029686800b7ce36bde4a55c815" title="32 bit unsigned integer">asUINT</a> index, <span class="keywordtype">void</span> *value);
  <span class="keywordtype">void</span> RemoveAt(<a class="code" href="angelscript_8h.html#ac8186f029686800b7ce36bde4a55c815" title="32 bit unsigned integer">asUINT</a> index);
  <span class="keywordtype">void</span> InsertLast(<span class="keywordtype">void</span> *value);
  <span class="keywordtype">void</span> RemoveLast();
  <span class="keywordtype">void</span> SortAsc();
  <span class="keywordtype">void</span> SortAsc(<a class="code" href="angelscript_8h.html#ac8186f029686800b7ce36bde4a55c815" title="32 bit unsigned integer">asUINT</a> index, <a class="code" href="angelscript_8h.html#ac8186f029686800b7ce36bde4a55c815" title="32 bit unsigned integer">asUINT</a> count);
  <span class="keywordtype">void</span> SortDesc();
  <span class="keywordtype">void</span> SortDesc(<a class="code" href="angelscript_8h.html#ac8186f029686800b7ce36bde4a55c815" title="32 bit unsigned integer">asUINT</a> index, <a class="code" href="angelscript_8h.html#ac8186f029686800b7ce36bde4a55c815" title="32 bit unsigned integer">asUINT</a> count);
  <span class="keywordtype">void</span> Reverse();
  <span class="keywordtype">int</span>  Find(<span class="keywordtype">void</span> *value);
  <span class="keywordtype">int</span>  Find(<a class="code" href="angelscript_8h.html#ac8186f029686800b7ce36bde4a55c815" title="32 bit unsigned integer">asUINT</a> index, <span class="keywordtype">void</span> *value);
};
</pre></div><h2><a class="anchor" id="doc_addon_array_2"></a>
Public script interface</h2>
<pre>
  class array&lt;class T&gt;
  {
    array();
    array(uint length);
    array(uint length, const T &amp;in defaultValue);</pre><pre>    T       &amp;opIndex(uint);
    const T &amp;opIndex(uint) const;</pre><pre>    array&lt;T&gt; opAssign(const array&lt;T&gt; &amp; in);</pre><pre>    uint length { get const; set; }</pre><pre>    void insertAt(uint index, const T&amp; in);
    void removeAt(uint index);
    void insertLast(const T&amp; in);
    void removeLast();
    uint length() const;
    void resize(uint);
    void sortAsc();
    void sortAsc(uint index, uint count);
    void sortDesc();
    void sortDesc(uint index, uint count);
    void reverse();
    int  find(const T&amp; in);
    int  find(uint index, const T&amp; in);
  }
</pre><h2><a class="anchor" id="doc_addon_array_3"></a>
Script example</h2>
<pre>
  int main()
  {
    array&lt;int&gt; arr = {1,2,3};</pre><pre>    int sum = 0;
    for( uint n = 0; n &lt; arr.length; n++ )
      sum += arr[n];</pre><pre>    return sum;
  }
</pre><h2><a class="anchor" id="doc_addon_array_4"></a>
C++ example</h2>
<p>This function shows how a script array can be instanciated from the application and then passed to the script.</p>
<div class="fragment"><pre class="fragment">CScriptArray *CreateArrayOfStrings()
{
  <span class="comment">// If called from the script, there will always be an active </span>
  <span class="comment">// context, which can be used to obtain a pointer to the engine.</span>
  <a class="code" href="classas_i_script_context.html" title="The interface to the virtual machine.">asIScriptContext</a> *ctx = <a class="code" href="angelscript_8h.html#ad3a20dc58093b92a5a44c7b6ada34a10" title="Returns the currently active context.">asGetActiveContext</a>();
  <span class="keywordflow">if</span>( ctx )
  {
    <a class="code" href="classas_i_script_engine.html" title="The engine interface.">asIScriptEngine</a>* engine = ctx-&gt;<a class="code" href="classas_i_script_context.html#a07f12016c5435aec5b63449abb6e4d8d" title="Returns a pointer to the engine.">GetEngine</a>();

    <span class="comment">// The script array needs to know its type to properly handle the elements</span>
    <a class="code" href="classas_i_object_type.html" title="The interface for an object type.">asIObjectType</a>* t = engine-&gt;<a class="code" href="classas_i_script_engine.html#a52f3e08240c9fc493793036721cbd5ce" title="Returns the object type interface for type.">GetObjectTypeById</a>(engine-&gt;<a class="code" href="classas_i_script_engine.html#ad1f6fecb0f53fd7966736b01f65c3dcb" title="Returns a type id by declaration.">GetTypeIdByDecl</a>(<span class="stringliteral">&quot;array&lt;string@&gt;&quot;</span>));

    CScriptArray* arr = <span class="keyword">new</span> CScriptArray(3, t);
    <span class="keywordflow">for</span>( <a class="code" href="angelscript_8h.html#ac8186f029686800b7ce36bde4a55c815" title="32 bit unsigned integer">asUINT</a> i = 0; i &lt; arr-&gt;GetSize(); i++ )
    {
      <span class="comment">// Get the pointer to the element so it can be set</span>
      CScriptString** p = <span class="keyword">static_cast&lt;</span>CScriptString**<span class="keyword">&gt;</span>(arr-&gt;At(i));
      *p = <span class="keyword">new</span> CScriptString(<span class="stringliteral">&quot;test&quot;</span>);
    }

    <span class="comment">// The ref count for the returned handle was already set in the array&#39;s constructor</span>
    <span class="keywordflow">return</span> arr;
  }
  <span class="keywordflow">return</span> 0;
}
</pre></div> </div></div>
</div>
  <div id="nav-path" class="navpath">
    <ul>

    <li class="footer">Generated on Sun Jan 29 2012 15:41:03 for AngelScript by
    <a href="http://www.doxygen.org/index.html">
    <img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.7.5.1 </li>
   </ul>
 </div>


</body>
</html>
